package com.example.xymarket.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xymarket.adapter.CartAdapter;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.httpUtil.OrderService;
import com.example.xymarket.httpUtil.UserAddressService;
import com.example.xymarket.R;
import com.example.xymarket.po.ShoppingCart;
import com.example.xymarket.po.UserAddress;
import com.example.xymarket.po.vo.OrderGoods;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BuyActivity extends AppCompatActivity {


    public OrderService orderService;
    public Spinner spinner;
    public ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
        setContentView(R.layout.activity_buy);
        MyHandler myHandler=new MyHandler(this,handler);
        UserAddressService userAddressService=new UserAddressService(myHandler);
        userAddressService.getAddress();
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
        updateListView(ShoppingCartActivity.shoppingCarts);
        int i=getIntent().getIntExtra("price",0);
        TextView total_price=(TextView)findViewById(R.id.total_price);
        total_price.setText(String.valueOf(i/100)+"." +String.valueOf(i%100)+"¥");
        orderService=new OrderService(myHandler);
    }

    public void updateListView(List<ShoppingCart> list)
    {
        if(list==null)return;
        ListView listView = (ListView) findViewById(R.id.listView);
        try {
            CartAdapter adapter = new CartAdapter(list,this,R.layout.item_shopping_cart,handler);
            listView.setAdapter(adapter);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }

    public List<UserAddress> userAddresses=new ArrayList<>();

    @SuppressLint("HandlerLeak")
    Handler handler=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch(msg.what)
            {
                case 1:
                {
                    userAddresses= (List<UserAddress>) msg.obj;
                    initSpinner(userAddresses);
                    break;
                }

                case 123:
                {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(),"成功",Toast.LENGTH_LONG).show();
                    Back();
                    break;
                }

                case -123:
                {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(),"失败",Toast.LENGTH_LONG).show();
                    Back();
                    break;
                }
            }
        }
    };


    public void Back()
    {
        Intent intent=new Intent(getApplicationContext(),OrderActivity.class);
        startActivity(intent);
    }
    public void Buy2(View view)
    {
        progressDialog = ProgressDialog.show(this, "提交中", "请稍等");
        int position=spinner.getSelectedItemPosition();
        int p=userAddresses.get(position).getId();
        List<OrderGoods> list=new ArrayList<>();
        for(ShoppingCart shoppingCart:ShoppingCartActivity.shoppingCarts)
        {
            OrderGoods orderGoods=new OrderGoods(shoppingCart.getGoodsEntity().getId(),shoppingCart.getNum(),p);
            list.add(orderGoods);
        }
        orderService.InsertOrder(list);

    }

    private void initSpinner(List<UserAddress> userAddresses) {
        List<String> choice = userAddresses.stream().map(userAddress->userAddress.getName()).collect(Collectors.toList());
        spinner=(Spinner)findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
                R.layout.simple_spinner_item,choice);
        //设置下拉样式以后显示的样式
        adapter.setDropDownViewResource(R.layout.my_drop_down_item);
        spinner.setAdapter(adapter);
    }
}